package com.me.libgdx_test;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class ActivityGameInfo implements Screen {
	
	ActivityMain game;
	Stage stage;
	SpriteBatch batch;
	ClassSkinReference skin;
	ClassLogDisplay logger;
	float width, height;
	Point dimensions;
	ClassPreviewActor preview_window;
	ClassReferenceDrawable references;
	final ClassSelectionDirector director;
	ClassAchievementInfo achievement_bar;
	//TextField textfield
	
	public ActivityGameInfo(final ActivityMain game){
		this.game = game;
		logger = new ClassLogDisplay("ActivityGameInfo");
		logger.print(String.valueOf(Gdx.graphics.getWidth()) + "x" + String.valueOf(Gdx.graphics.getHeight()));
		width = Gdx.graphics.getWidth();
		height = Gdx.graphics.getHeight();
		dimensions = new Point((int)width,(int)height);
		
		batch = new SpriteBatch();
		stage = new Stage();
		skin = game.skin;
		
		/*table.add(title_label).size(width/3,height/8).padBottom(height/15);
		table.row();
		table.row();*/
		
		final Label title_label = new Label("Game Info", skin.get_skin());
		title_label.setPosition(5*width/20, 5*height/6);
		title_label.setSize(width/10, height/10);
		title_label.setFontScale(10);

		Label detail_field = new Label("", skin.get_skin());
		detail_field.setPosition(29*width/40, 3*height/24);
		detail_field.setSize(4*width/20,3*height/10);
		detail_field.setFontScale(1.3f);
		detail_field.setWrap(true);

		director = new ClassSelectionDirector(title_label,detail_field,true,2);
		
		init_things();
		
		logger.print("Game Info");
		
		Gdx.input.setInputProcessor(stage);
	}
	
	public void init_things(){
		logger.print("init things");
		init_classes();
		init_actors();
	}
	
	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
		stage.draw();
		Table.drawDebug(stage);
		
	}

	@Override
	public void resize(int width, int height) {
		stage.setViewport(width, height, false);
		
	}

	@Override
	public void show() {
		director.reset_index();
		Gdx.input.setInputProcessor(stage);
		init_actors();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {
		stage.dispose();
		//skin.get_skin().dispose();
	}
	class Position{
		Point max_pos, starting_pos, increment;
		Point current_position;
		Position(Point max_pos, Point starting_pos, Point increment){
			this.max_pos = max_pos;
			this.starting_pos = starting_pos;
			this.increment = increment;
			current_position = new Point();
		}
		public void next_position(int order){
			current_position = new Point(starting_pos);
			for(int i = 0; i<order;i++){
				ClassLogDisplay.print(current_position);
				if(current_position.x + (increment.x * 1.5) < max_pos.x){
					current_position.x += increment.x;
				}else{
					current_position.x = starting_pos.x;
					current_position.y += increment.y;
				}
			}
			ClassLogDisplay.print("Returning...");
			ClassLogDisplay.print(current_position);
		}
		public Point get_current_position(){
			return current_position;
		}
	}
	
	public void init_buttons(){
		logger.print("init buttons");
		int button_width = (int) (20*width/100);
		int button_height = (int) (height/10);
		Position position = new Position(new Point((int)width,(int)height), new Point((int)(5*width)/100, (int)(56*height)/100), new Point((int)(23*width)/100,(int)button_height));
		
		final TextButton Back_Button = new TextButton("Back", skin.get_button_skin());
		position.next_position(0);
		Back_Button.setPosition(position.get_current_position().x, position.get_current_position().y);
		Back_Button.setSize(button_width, button_height);
		//table.add(Tavern_Button).size(button_width,button_height).align(Align.left);
		Back_Button.addListener(new ChangeListener() {
			public void changed (ChangeEvent event, Actor actor) {
				game.setScreen(game.indexScreen);
			}
		});
		stage.addActor(Back_Button);
		
		
		final TextButton Spell_Button = new TextButton("Spells", skin.get_button_skin());
		position.next_position(1);
		Spell_Button.setPosition(position.get_current_position().x, position.get_current_position().y);
		Spell_Button.setSize(button_width, button_height);
		Spell_Button.addListener(new ChangeListener() {
			public void changed (ChangeEvent event, Actor actor) {
				director.set_index(5);
				//director.set_current();
				init_actors();
			}
		});
		stage.addActor(Spell_Button);
		
		final TextButton Market_Button = new TextButton("Levels", skin.get_button_skin());
		position.next_position(2);
		Market_Button.setPosition(position.get_current_position().x, position.get_current_position().y);
		Market_Button.setSize(button_width, button_height);
		Market_Button.addListener(new ChangeListener() {
			public void changed (ChangeEvent event, Actor actor) {
				director.set_index(1);
				//director.set_current();
				init_actors();
			}
		});
		stage.addActor(Market_Button);
		
		final TextButton Race_Button = new TextButton("Races", skin.get_button_skin());
		position.next_position(3);
		Race_Button.setPosition(position.get_current_position().x, position.get_current_position().y);
		Race_Button.setSize(button_width, button_height);
		Race_Button.addListener(new ChangeListener() {
			public void changed (ChangeEvent event, Actor actor) {
				director.set_index(0);
				//director.set_current();
				init_actors();
			}
		});
		stage.addActor(Race_Button);
		
		final TextButton Tavern_Button = new TextButton("Classes", skin.get_button_skin());
		position.next_position(4);
		Tavern_Button.setPosition(position.get_current_position().x, position.get_current_position().y);
		Tavern_Button.setSize(button_width, button_height);
		//table.add(Tavern_Button).size(button_width,button_height).align(Align.left);
		Tavern_Button.addListener(new ChangeListener() {
			public void changed (ChangeEvent event, Actor actor) {
				director.set_index(2);
				//director.set_current();
				init_actors();
			}
		});
		stage.addActor(Tavern_Button);
		
		final TextButton Enemy_Button = new TextButton("Enemies", skin.get_button_skin());
		position.next_position(5);
		Enemy_Button.setPosition(position.get_current_position().x, position.get_current_position().y);
		Enemy_Button.setSize(button_width, button_height);
		Enemy_Button.addListener(new ChangeListener() {
			public void changed (ChangeEvent event, Actor actor) {
				director.set_index(4);
				//director.set_current();
				init_actors();
			}
		});
		stage.addActor(Enemy_Button);
		
		final TextButton Mod_Button = new TextButton("Mods", skin.get_button_skin());
		position.next_position(6);
		Mod_Button.setPosition(position.get_current_position().x, position.get_current_position().y);
		Mod_Button.setSize(button_width, button_height);
		Mod_Button.addListener(new ChangeListener() {
			public void changed (ChangeEvent event, Actor actor) {
				director.set_index(3);
				//director.set_current();
				init_actors();
			}
		});
		stage.addActor(Mod_Button);
		
		final TextButton God_Button = new TextButton("Gods", skin.get_button_skin());
		position.next_position(7);
		God_Button.setPosition(position.get_current_position().x, position.get_current_position().y);
		God_Button.setSize(button_width, button_height);
		God_Button.addListener(new ChangeListener() {
			public void changed (ChangeEvent event, Actor actor) {
				director.set_index(6);
				//director.set_current();
				init_actors();
			}
		});
		stage.addActor(God_Button);
	}
	
	public void init_classes(){
		ClassTexturePackerLoader sprite_textures = game.sprite_textures;
		references = new ClassReferenceDrawable(0,0,0,game.unlockable);
		ClassGalleryActor classes = new ClassGalleryActor(sprite_textures,
				new Point((int)width/3,(int)(4*height/6)),
				new Point((int)(2*width/20),(int)(9*height/20)),
				8,3);
		classes.set_references(references.classObjects);
		ClassGalleryActor races = new ClassGalleryActor(sprite_textures,
				new Point((int)width/3,(int)(3*height/6)),
				new Point((int)(2*width/20),(int)(7*height/20)),
				5,5);
		races.set_references(references.raceObjects);
		ClassGalleryActor levels = new ClassGalleryActor(sprite_textures,
				new Point((int)width/3,(int)(3*height/6)),
				new Point((int)(2*width/20),(int)(7*height/20)),
				5,5);
		levels.set_references(references.levels_w_description());
		ClassGalleryActor mods = new ClassGalleryActor(sprite_textures,
				new Point((int)width/3,(int)(3*height/6)),
				new Point((int)(2*width/20),(int)(8*height/20)),
				6,4);
		mods.set_references(references.modObjects);
		ClassGalleryActor enemies = new ClassGalleryActor(sprite_textures,
				new Point((int)width/3,(int)(4*height/6)),
				new Point((int)(2*width/20),(int)(8*height/20)),
				8,3);
		enemies.set_references(references.enemyObjects);
		ClassGalleryActor spells = new ClassGalleryActor(sprite_textures,
				new Point((int)width/3,(int)(3*height/6)),
				new Point((int)(2*width/20),(int)(8*height/20)),
				6,4);
		spells.set_references(references.spellObjects);
		ClassGalleryActor gods = new ClassGalleryActor(sprite_textures,
				new Point((int)width/3,(int)(3*height/6)),
				new Point((int)(2*width/20),(int)(7*height/20)),
				5,5);
		gods.set_references(references.godObjects);
		
		director.set_actors(references.levelObjects,races,levels,classes,mods, enemies, spells, gods, game.unlockable.get_unlockable_prototypes().get_factory());
	}
	
	public void init_actors(){
		//ArrayList<Sprite> sprites = new ArrayList<Sprite>();
		//sprites.add(sprite_textures.find_by_name("class"));

		stage.clear();
		
		init_buttons();
		
		stage.addActor(director.get_title());
		stage.addActor(director.get_description());
		
		//The preview window
		preview_window = new ClassPreviewActor(new Point((int)(10*width)/20,(int)(4*height/100)),
				new Point((int)width/5,(int)(3*height/6)));
		
		//The smaller select windows
		//ClassGalleryActor classes = new ClassGalleryActor(sprite_textures.return_all_by_name("class"),
		//		new Point((int)width/3,(int)(4*height/6)),new Point((int)width/20,(int)(13*height/20)),7,3);
		stage.addActor(preview_window);
		
		director.set_vars(0,
				director.get_gallery().get_actors().get(0).get_name(),
				director.get_gallery().get_actors().get(0).get_description());
		director.set_label_text(false,0);
		preview_window.set_sprite(director.get_gallery().get_actors().get(0).get_sprite());
		
		//ClassGalleryActor classes = new ClassGalleryActor(sprites);
		for(final ClassActorFactory actor: director.get_gallery().get_actors()){
			final Sprite sprite = actor.get_sprite();
			//director.set_text(director.get_gallery().get_actors().indexOf(actor),actor.get_name(),actor.get_description());
			actor.addListener( new ClickListener() {     
				public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
			    {
					director.set_vars(director.get_gallery().get_actors().indexOf(actor),actor.get_name(),actor.get_description());
					director.set_label_text(false,0);
			    	preview_window.set_sprite(sprite);
			        return true;
			    }
			});
			stage.addActor(actor);
		}
		
		logger.print("before achievebar");
		
		achievement_bar = new ClassAchievementInfo(game,dimensions);
		
		stage.addActor(achievement_bar.get_achievement_group());
		
		logger.print("after achievebar");
	}

}
